package com.yunshu.yunshuapp.zixunlist;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yunshu.yunshuapp.OnTapListener;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.WebviewActivity;
import com.yunshu.yunshuapp.WebviewWithShareActivity;
import com.yunshu.yunshuapp.banner.ScrollBanner;
import com.yunshu.yunshuapp.network.YSRequest;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwenjie on 2016/12/22.
 */
public class ZixunListAdapter extends RecyclerView.Adapter implements OnTapListener {

    private final int RecyclerViewItemTypeHeader = 1;
    private final int RecyclerViewItemTypeCommon = 2;
    private final int RecyclerViewItemTypeBanner = 3;

    private final ImageLoader mImageLoader;
    private final DisplayImageOptions options;
    private ArrayList<ZixunListModel> models;
    public OnTapListener tapListener;
    public Activity activity;
    private View banner;

    public ZixunListAdapter(Context context){
        // 使用ImageLoader之前初始化
        YSUtils.initImageLoader(context);

        models = new ArrayList<>();
        // 获取图片加载实例
        mImageLoader = ImageLoader.getInstance();
        options = YSUtils.displayImageOptions();

        tapListener = this;
    }

    @Override
    public int getItemCount() {

        int count = models.size();
        return count ;
    }

    @Override
    public int getItemViewType(int position) {

        ZixunListModel model = models.get(position);
        if (model.isHeader()){
            return RecyclerViewItemTypeHeader;
        }else if(model.isBanner()){
            return RecyclerViewItemTypeBanner;
        }else{
            return RecyclerViewItemTypeCommon;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == RecyclerViewItemTypeCommon) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item1,parent,false);
            ListViewHolder holder = new ListViewHolder(view);
            return holder;
        }else if(viewType == RecyclerViewItemTypeBanner){

            ListViewHolder holder = new ListViewHolder(banner);
            return holder;
        }else{
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_header,parent,false);
            ListHeaderHolder holder = new ListHeaderHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ZixunListModel model = models.get(position);
        if (model.isHeader()){
            ((ListHeaderHolder)holder).setPosition(position);
            ((ListHeaderHolder)holder).titleTV.setText(model.getTitle());
            ((ListHeaderHolder)holder).imgView.setImageResource(model.getImagePath());
        }else if(model.isBanner()) {

        }else {
            ((ListViewHolder)holder).setPosition(position);

            ListViewHolder listHolder = (ListViewHolder) holder;
            mImageLoader.displayImage(model.getImageUrl(), listHolder.imgView);
            listHolder.titleTV.setText(model.getTitle());
            listHolder.dateTV.setText(model.getDate());
            listHolder.favourateTV.setText(model.getFavourateCount());
        }

    }

    @Override
    public void onSingleTap(View v, int position) {

        ZixunListModel model = models.get(position);
        if(model.isHeader()){
            Intent intent = new Intent();
            intent.setClass(this.activity, ZixunListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("listtype",model.listId);
            intent.putExtras(bundle);
            activity.startActivityForResult(intent,0);
        }else{
            Intent intent = new Intent();
            intent.setClass(this.activity, WebviewWithShareActivity.class);
            Bundle bundle = new Bundle();
            String url = YSRequest.BASE_URL + YSRequest.ZIXUN_Detail + model.getModelId();
            bundle.putString("url",url);
            bundle.putString("title","资讯详情");
            String zixun = model.serialize();
            if (zixun != null)
                bundle.putString("zixun",zixun);
            intent.putExtras(bundle);
            this.activity.startActivityForResult(intent,0);
        }
    }

    public void setBanner(ScrollBanner banner) {
        this.banner = banner;
        this.addBanner2Model();
        notifyDataSetChanged();
    }

    public void AddFooterItem(List<ZixunListModel> footerDatas) {
        models.addAll(footerDatas);
        notifyDataSetChanged();
    }

    class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView imgView;
        TextView titleTV;
        TextView dateTV;
        TextView favourateTV;
        int mPosition;

        public ListViewHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            titleTV = (TextView) itemView.findViewById(R.id.textView1);
            dateTV = (TextView) itemView.findViewById(R.id.textView2);
            favourateTV = (TextView) itemView.findViewById(R.id.loveCountTV);
        }

        public void setPosition(int position) {
            mPosition = position;
        }


        public void onClick(View v) {
            tapListener.onSingleTap(v,mPosition);
        //    Toast.makeText(MainActivity.this, "click "+mPosition, 0).show();

        }
    }

    class BannerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ScrollBanner banner;

        public BannerHolder(ScrollBanner itemView ) {
            super(itemView);
            banner = itemView;
        }


        @Override
        public void onClick(View view) {

        }
    }

    class ListHeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView imgView;
        TextView titleTV;
        Button button;
        int mPosition;

        public ListHeaderHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            titleTV = (TextView) itemView.findViewById(R.id.title);
            button = (Button) itemView.findViewById(R.id.button);
            button.setOnClickListener(this);
        }

        public void setPosition(int position) {
            mPosition = position;
        }

        public void onClick(View v) {
            tapListener.onSingleTap(v,mPosition);
            //    Toast.makeText(MainActivity.this, "click "+mPosition, 0).show();

        }
    }

    public void setModels(List<ZixunListModel> newModels){
        models.clear();
        addBanner2Model();
        models.addAll(newModels);
    }


    private void addBanner2Model() {
        if (this.banner == null ) return;
        if (models.size() == 0 ){
            ZixunListModel bannerModel = new ZixunListModel();
            bannerModel.setBanner(true);
            models.add(bannerModel);
            return;
        }
        ZixunListModel firstModel = models.get(0);
        if (!firstModel.isBanner()){
            ZixunListModel bannerModel = new ZixunListModel();
            bannerModel.setBanner(true);
            models.add(bannerModel);
            return;
        }
    }


}
